package cn.lrwjz.androidguide.ui;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.card.MaterialCardView;

import cn.lrwjz.androidguide.R;

public class CardActivity extends AppCompatActivity {
    private TextView expandableContent;
    private MaterialCardView draggableCard;
    private NestedScrollView scrollView;
    private float dX, dY;
    private float lastTouchX, lastTouchY;
    private boolean isExpanded = false;
    private boolean isDragging = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);

        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Card 示例");
        }

        scrollView = findViewById(R.id.scrollView);

        // 设置基础卡片和媒体卡片点击事件
        MaterialButton btnAction1 = findViewById(R.id.btnAction1);
        MaterialButton btnAction2 = findViewById(R.id.btnAction2);
        MaterialButton btnAction3 = findViewById(R.id.btnAction3);
        MaterialButton btnAction4 = findViewById(R.id.btnAction4);

        btnAction1.setOnClickListener(v -> 
            Toast.makeText(this, "点击了基础卡片按钮", Toast.LENGTH_SHORT).show());

        btnAction2.setOnClickListener(v -> 
            Toast.makeText(this, "点击了媒体卡片按钮", Toast.LENGTH_SHORT).show());

        // 设置可展开卡片
        expandableContent = findViewById(R.id.expandableContent);
        expandableContent.setVisibility(View.GONE);

        btnAction3.setOnClickListener(v -> {
            try {
                if (isExpanded) {
                    // 折叠内容
                    ValueAnimator animator = ValueAnimator.ofInt(expandableContent.getHeight(), 0);
                    animator.addUpdateListener(animation -> {
                        int value = (int) animation.getAnimatedValue();
                        ViewGroup.LayoutParams layoutParams = expandableContent.getLayoutParams();
                        layoutParams.height = value;
                        expandableContent.setLayoutParams(layoutParams);
                    });
                    animator.setDuration(300);
                    animator.start();
                    expandableContent.setVisibility(View.GONE);
                    btnAction3.setText("展开");
                } else {
                    // 展开内容
                    expandableContent.setVisibility(View.VISIBLE);
                    expandableContent.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    int targetHeight = expandableContent.getMeasuredHeight();
                    
                    ViewGroup.LayoutParams layoutParams = expandableContent.getLayoutParams();
                    layoutParams.height = 0;
                    expandableContent.setLayoutParams(layoutParams);

                    ValueAnimator animator = ValueAnimator.ofInt(0, targetHeight);
                    animator.addUpdateListener(animation -> {
                        int value = (int) animation.getAnimatedValue();
                        layoutParams.height = value;
                        expandableContent.setLayoutParams(layoutParams);
                    });
                    animator.setDuration(300);
                    animator.start();
                    btnAction3.setText("折叠");
                }
                isExpanded = !isExpanded;
            } catch (Exception e) {
                Toast.makeText(this, "展开/折叠操作失败", Toast.LENGTH_SHORT).show();
            }
        });

        // 设置可拖动卡片
        draggableCard = findViewById(R.id.draggableCard);
        btnAction4.setOnClickListener(v -> 
            Toast.makeText(this, "长按卡片并拖动来移动位置", Toast.LENGTH_SHORT).show());

        draggableCard.setOnTouchListener((view, event) -> {
            ViewGroup parent = (ViewGroup) view.getParent();
            
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // 记录初始触摸位置
                    lastTouchX = event.getRawX();
                    lastTouchY = event.getRawY();
                    dX = view.getX();
                    dY = view.getY();
                    // 禁用滚动
                    scrollView.requestDisallowInterceptTouchEvent(true);
                    // 开始拖动效果
                    view.animate()
                        .scaleX(1.05f)
                        .scaleY(1.05f)
                        .translationZ(25f)
                        .setDuration(200)
                        .start();
                    isDragging = true;
                    return true;

                case MotionEvent.ACTION_MOVE:
                    if (!isDragging) return false;
                    
                    // 计算移动距离
                    float deltaX = event.getRawX() - lastTouchX;
                    float deltaY = event.getRawY() - lastTouchY;
                    
                    // 更新位置
                    float newX = dX + deltaX;
                    float newY = dY + deltaY;
                    
                    // 限制范围
                    newX = Math.max(0, Math.min(newX, parent.getWidth() - view.getWidth()));
                    newY = Math.max(0, Math.min(newY, parent.getHeight() - view.getHeight()));
                    
                    view.setX(newX);
                    view.setY(newY);
                    return true;

                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    // 启用滚动
                    scrollView.requestDisallowInterceptTouchEvent(false);
                    // 结束拖动效果
                    view.animate()
                        .scaleX(1f)
                        .scaleY(1f)
                        .translationZ(0f)
                        .setDuration(200)
                        .start();
                    isDragging = false;
                    return true;
            }
            return false;
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 